import pandas as pd
from fastcache import lru_cache
from .Tdatabase import *
from dateutil.parser import parse
import datetime as dt

@lru_cache()
def get_ind_eq(my):
    if my[0] != 'C':
        sql = "select * from AIndexEODPrices where S_INFO_WINDCODE='{0}' and TRADE_DT >'20040101' and S_DQ_OPEN is not null and S_DQ_PRECLOSE is not null  order by TRADE_DT ".format(
            my)
        df = pd.read_sql(sql, windengine)
    else:
        sql = "select * from AINDEXINDUSTRIESEODCITICS where S_INFO_WINDCODE='{0}' and TRADE_DT >'20040101' and S_DQ_OPEN is not null and S_DQ_PRECLOSE is not null  order by TRADE_DT ".format(
            my)
        df = pd.read_sql(sql, windengine)

    return df



def time2unix(t):
    """
    将时间转换为unix时间戳
    """
    t = parse(t) if isinstance(t, str) else t
    return (int(dt.datetime.timestamp(t)) + 28800) * 1000


